package kcn.ip21.actions;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import kcn.ip21.dbservice.DbService;
import kcn.ip21.model.Dutys1;
import kcn.ip21.model.Dutys3;
import kcn.ip21.model.Dutys4;
import kcn.ip21.model.Dutys6;
import kcn.ip21.model.Ip21s1;
import kcn.ip21.model.Ip21s3;
import kcn.ip21.model.Ip21s4;
import kcn.ip21.model.Ip21s6;

import com.opensymphony.xwork2.ActionSupport;

public class SaveTagAction extends ActionSupport {
	private String shop6_stoptime;
	private String shop6_siloNo;
	private String shop6_cementtype;
	private String shop6_separatorHole;
	private String shop4_gunsize;
	private String shop4_doorangle;
	private String shop4_pump;
	private String shop4_wind;
	private String shop3_stoptime;
	private String shop3_separatorHole;
	private String shop3_silo01;
	private String shop3_silo02;
	private String shop3_silo03;
	private String shop3_silo04;
	private String shop1_stoptime;
	private String shop1_siloNo;
	private String shop1_cementtype;
	private String shop1_loadingT;
	private String shop1_slidegate;
	private String staffname;
	private DbService dbs;
	private Map map;
	private Date time;
	private Ip21s6 ip21s6;
	private Dutys6 dutys6;
	private Ip21s4 ip21s4;
	private Dutys4 dutys4;

	// public static void main(String args[]) {
	// SaveTagAction sta = new SaveTagAction();
	// try {
	// sta.execute();
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub\

		dbs = new DbService();
		map = new HashMap<String, String>();
		List<String> tagslist;
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy HH:mm:ss",
				Locale.US);
		time = new Date();
		String ip21time = sdf.format(time);
		/*
		 * 为仓库添加记录
		 */
		dutys6 = new Dutys6();
		dutys6.setStaffname(staffname);
		dutys6.setCementtype(shop6_cementtype);
		dutys6.setSeparatorHole(shop6_separatorHole);
		dutys6.setSiloNo(shop6_siloNo);
		dutys6.setStoptime(shop6_stoptime);
		dutys6.setSubmittime(time);
		ip21s6 = new Ip21s6();
		tagslist = this.gettags(ip21s6);
		map.clear();
		map = dbs.getdatafromIP21(tagslist, ip21time);
		ip21s6 = (Ip21s6) this.assembling(map, ip21s6, tagslist);
		dutys6.setIp21s6(ip21s6);
		System.out.println("*******************************************************");
		System.out.println(shop6_siloNo);
		dbs.save(ip21s6);
		dbs.save(dutys6);
		/*
		 * 为仓库4添加记录
		 */
		dutys4 = new Dutys4();
		ip21s4 = new Ip21s4();
		tagslist = this.gettags(ip21s4);
		map.clear();
		map = dbs.getdatafromIP21(tagslist, ip21time);
		ip21s4 = (Ip21s4) this.assembling(map, ip21s4, tagslist);
		dutys4.setIp21s4(ip21s4);
		dutys4.setPump(shop4_pump);
		dutys4.setDoorangle(shop4_doorangle);
		dutys4.setGunsize(shop4_gunsize);
		dutys4.setStaffname(staffname);
		dutys4.setSubmittime(time);
		dutys4.setWind(shop4_wind);
		dbs.save(ip21s4);
		dbs.save(dutys4);
		/**
		 * 为仓库3添加记录
		 */
		Dutys3 dutys3=new Dutys3();
		Ip21s3 ip21s3=new Ip21s3();
		tagslist=this.gettags(ip21s3);
		map.clear();
		map=dbs.getdatafromIP21(tagslist, ip21time);
		ip21s3=(Ip21s3)this.assembling(map, ip21s3, tagslist);
		dutys3.setIp21s3(ip21s3);
		dutys3.setStoptime(shop3_stoptime);
		dutys3.setSeparatorHole(shop3_separatorHole);
		dutys3.setSubmittime(time);
		dutys3.setSilo01(shop3_silo01);
		dutys3.setSilo02(shop3_silo02);
		dutys3.setSilo03(shop3_silo03);
		dutys3.setSilo04(shop3_silo04);
		dbs.save(ip21s3);
		dbs.save(dutys3);
		/**
		 * 为仓库1添加记录
		 */
		Dutys1 dutys1=new Dutys1();
		Ip21s1 ip21s1=new Ip21s1();
		tagslist=this.gettags(ip21s1);
		map.clear();
		map=dbs.getdatafromIP21(tagslist, ip21time);
		ip21s1=(Ip21s1)this.assembling(map,ip21s1,tagslist);
		dutys1.setIp21s1(ip21s1);
		dutys1.setCementtype(shop1_cementtype);
		dutys1.setLoadingT(shop1_loadingT);
		dutys1.setSiloNo(shop1_siloNo);
		dutys1.setSlidegate(shop1_slidegate);
		dutys1.setStoptime(shop1_stoptime);
		dutys1.setSubmittime(time);
		dbs.save(ip21s1);
		dbs.save(dutys1);
		return super.execute();
	}

	public List<String> gettags(Object obj) {
		List<String> tagsList = new ArrayList<String>();
		tagsList.clear();
		Field[] fields;
		fields = obj.getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
//			System.out.println(fields[i].getName());
			if (fields[i].getName().startsWith("shop")) {
				tagsList.add(fields[i].getName().substring(5));
			}
		}
		return tagsList;
	}

	public Object assembling(Map<String, String> map, Object obj, List tagsname) {
		for (int i = 0; i < tagsname.size(); i++) {
			String methodname = "setShop_" + tagsname.get(i);
			String methodValue = map.get(tagsname.get(i));
			Method method = null;
			try {
				method = obj.getClass().getMethod(methodname, String.class);
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				method.invoke(obj, methodValue);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return obj;
	}

	public String getShop6_stoptime() {
		return shop6_stoptime;
	}

	public void setShop6_stoptime(String shop6_stoptime) {
		this.shop6_stoptime = shop6_stoptime;
	}

	public String getShop6_siloNo() {
		return shop6_siloNo;
	}

	public void setShop6_siloNo(String shop6_siloNo) {
		this.shop6_siloNo = shop6_siloNo;
	}

	public String getShop6_cementtype() {
		return shop6_cementtype;
	}

	public void setShop6_cementtype(String shop6_cementtype) {
		this.shop6_cementtype = shop6_cementtype;
	}

	public String getShop6_separatorHole() {
		return shop6_separatorHole;
	}

	public void setShop6_separatorHole(String shop6_separatorHole) {
		this.shop6_separatorHole = shop6_separatorHole;
	}

	public String getStaffname() {
		return staffname;
	}

	public void setStaffname(String staffname) {
		this.staffname = staffname;
	}

	public String getShop4_gunsize() {
		return shop4_gunsize;
	}

	public void setShop4_gunsize(String shop4_gunsize) {
		this.shop4_gunsize = shop4_gunsize;
	}

	public String getShop4_doorangle() {
		return shop4_doorangle;
	}

	public void setShop4_doorangle(String shop4_doorangle) {
		this.shop4_doorangle = shop4_doorangle;
	}

	public String getShop4_pump() {
		return shop4_pump;
	}

	public void setShop4_pump(String shop4_pump) {
		this.shop4_pump = shop4_pump;
	}

	public String getShop4_wind() {
		return shop4_wind;
	}

	public void setShop4_wind(String shop4_wind) {
		this.shop4_wind = shop4_wind;
	}

	public String getShop3_stoptime() {
		return shop3_stoptime;
	}

	public void setShop3_stoptime(String shop3_stoptime) {
		this.shop3_stoptime = shop3_stoptime;
	}

	public String getShop3_separatorHole() {
		return shop3_separatorHole;
	}

	public void setShop3_separatorHole(String shop3_separatorHole) {
		this.shop3_separatorHole = shop3_separatorHole;
	}

	public String getShop3_silo01() {
		return shop3_silo01;
	}

	public void setShop3_silo01(String shop3_silo01) {
		this.shop3_silo01 = shop3_silo01;
	}

	public String getShop3_silo02() {
		return shop3_silo02;
	}

	public void setShop3_silo02(String shop3_silo02) {
		this.shop3_silo02 = shop3_silo02;
	}

	public String getShop3_silo03() {
		return shop3_silo03;
	}

	public void setShop3_silo03(String shop3_silo03) {
		this.shop3_silo03 = shop3_silo03;
	}

	public String getShop3_silo04() {
		return shop3_silo04;
	}

	public void setShop3_silo04(String shop3_silo04) {
		this.shop3_silo04 = shop3_silo04;
	}

	public String getShop1_stoptime() {
		return shop1_stoptime;
	}

	public void setShop1_stoptime(String shop1_stoptime) {
		this.shop1_stoptime = shop1_stoptime;
	}

	public String getShop1_siloNo() {
		return shop1_siloNo;
	}

	public void setShop1_siloNo(String shop1_siloNo) {
		this.shop1_siloNo = shop1_siloNo;
	}

	public String getShop1_cementtype() {
		return shop1_cementtype;
	}

	public void setShop1_cementtype(String shop1_cementtype) {
		this.shop1_cementtype = shop1_cementtype;
	}

	public String getShop1_loadingT() {
		return shop1_loadingT;
	}

	public void setShop1_loadingT(String shop1_loadingT) {
		this.shop1_loadingT = shop1_loadingT;
	}

	public String getShop1_slidegate() {
		return shop1_slidegate;
	}

	public void setShop1_slidegate(String shop1_slidegate) {
		this.shop1_slidegate = shop1_slidegate;
	}
}
